 host="127.0.0.1"
 port="3306"
 user="root"
 passwd="root123456"
 dbname="test"

# 建表前先删表
 sql1="drop table if exists $1" 
mysql -h$host -P$port -u$user -p$passwd $dbname -e "$sql1"

# 确定没有同名的表后建表
# 首先获取第一行
names=`head -n +1 $2 | sed 's/,/ /g'`
# 找出最后一个列名
lastname=`echo "$names" | awk '{print $NF}'`
sql2="create table if not exists $1("
    for i in $names
    do
        if [ $i == "$lastname" ]
        then
            sql2="$sql2$i varchar(200))"
        else
            sql2="$sql2$i varchar(200),"
        fi
    done

mysql -h$host -P$port -u$user -p$passwd $dbname -e "$sql2"

# 导入数据
sql3="LOAD DATA INFILE '$2' INTO TABLE $1
CHARACTER SET utf8
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES"
mysql -h$host -P$port -u$user -p$passwd $dbname -e "$sql3"

# 查看表和数据
sql4="select * from $1"
mysql -h$host -P$port -u$user -p$passwd $dbname -e "$sql4"